package com.guet.careapp.controller.rest.controller.service;

import com.google.gson.Gson;
import com.guet.careapp.controller.rest.common.errorCode.Error;
import com.guet.careapp.controller.rest.common.errorCode.ErrorCodeFactory;
import com.guet.careapp.dao.IAlarmClockDao;
import com.guet.careapp.pojo.TServiceAlarmclockInfo;
import com.guet.careapp.pojo.TUserUnderGuardiansInfo;
import com.guet.careapp.pojo.rest.AjaxReturn;
import com.guet.careapp.service.IAlarmClockSercice;
import com.guet.careapp.service.IUserUnderGuardiansInfoService;
import com.guet.careapp.utils.ConvertUtils;
import com.guet.careapp.utils.TimeHelper;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Map;

/**
 * @author LXM
 * @Title: ${file_name}
 * @Description: ${todo}
 * @date 2018/3/19下午3:33
 */
@Controller
@Path("/services/alarm")
public class Alarm {

    protected Logger logger = LoggerFactory.getLogger(Alarm.class);

    @Autowired
    IAlarmClockSercice alarmClockSercice;

    @Autowired
    IUserUnderGuardiansInfoService userUnderGuardiansInfoService;

    @POST
    @Path("/add")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)

    public String addAlarmClock(String json) {

        Map map = ConvertUtils.jsonToMap(json);
        String ugid;
        String infomation;
        TUserUnderGuardiansInfo underGuardiansInfo;
        TServiceAlarmclockInfo alarmclockInfo = null;
        try {
            infomation = (String) map.get("infomation");
            ugid = (String) map.get("ugid");
            int id = Integer.parseInt(ugid);
            Map fieldMap = new HashedMap();
            fieldMap.put("ug_id", id);
            underGuardiansInfo = userUnderGuardiansInfoService.findByField(fieldMap);
            if (underGuardiansInfo == null) {
                return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.NotFond).getCodeNum(), "被监护人不存在"));
            }
        } catch (Exception e) {
            return new Gson().toJson(new AjaxReturn((ErrorCodeFactory.get(Error.TypeError)).getCodeNum(), "参数类型异常"));
        }

        try {
            Map filedMap = new HashedMap();
            filedMap.put("ug_id", ugid);
            TServiceAlarmclockInfo info = alarmClockSercice.findByField(filedMap);
            if (info == null) {
                info = new TServiceAlarmclockInfo();
                info.setTime(TimeHelper.getTime());
                info.setInfomation(infomation);
                info.setUgId(underGuardiansInfo);
                alarmClockSercice.save(info);
            } else {
                info.setInfomation(infomation);
                info.setTime(TimeHelper.getTime());
                alarmClockSercice.saveOrUpdate(info);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.Unkonwn).getCodeNum(), "未知异常"));

        }

        return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.Success).getCodeNum(), "成功"));

    }


    @GET
    @Path("/{ugid}")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAlarmInfo(@PathParam("ugid") final String ugid) {

        int id;
        TUserUnderGuardiansInfo userUnderGuardiansInfo = null;
        try {
            id = Integer.parseInt(ugid);
            Map fieldMap = new HashedMap();
            fieldMap.put("ug_id", id);
            userUnderGuardiansInfo = userUnderGuardiansInfoService.findByField(fieldMap);
            if (userUnderGuardiansInfo == null) {
                return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.NotFond).getCodeNum(), "被监护人不存在"));
            }
        } catch (Exception e) {
            return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.TypeError).getCodeNum(), "参数类型异常"));
        }
        try {
            Map filedMap = new HashedMap();
            filedMap.put("ug_id", ugid);
            TServiceAlarmclockInfo info = alarmClockSercice.findByField(filedMap);
            if (info == null) {
                return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.NotFond).getCodeNum(), "记录不存在，请先添加记录"));
            } else {
                filedMap.clear();

                return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.Success).getCodeNum(), "成功", info.getInfomation()));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.Unkonwn).getCodeNum(), "未知异常"));

        }
    }


    @DELETE
    @Path("/{ugid}")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteAlarmInfo(@PathParam("ugid") final String ugid) {
        int id;
        TUserUnderGuardiansInfo userUnderGuardiansInfo = null;
        try {
            id = Integer.parseInt(ugid);
            Map fieldMap = new HashedMap();
            fieldMap.put("ug_id", id);
            userUnderGuardiansInfo = userUnderGuardiansInfoService.findByField(fieldMap);
            if (userUnderGuardiansInfo == null) {
                return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.NotFond).getCodeNum(), "被监护人不存在"));
            }
        } catch (Exception e) {
            return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.TypeError).getCodeNum(), "参数类型异常"));
        }
        try {
            Map filedMap = new HashedMap();
            filedMap.put("ug_id", ugid);
            TServiceAlarmclockInfo info = alarmClockSercice.findByField(filedMap);
            if (info == null) {
                return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.NotFond).getCodeNum(), "记录不存在，请先添加记录"));
            } else {
                alarmClockSercice.deleteById(info.getId());
                return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.Success).getCodeNum(), "成功"));
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Gson().toJson(new AjaxReturn(ErrorCodeFactory.get(Error.Unkonwn).getCodeNum(), "未知异常"));

        }

    }
}
